import cv2 as cv
import argparse
import numpy as np
import time
import requests

def get(url):
    header = {'Content-Type':'application/json'}
    response = requests.get(url,header)
    return response.json()

# Initialize the parameters
confThreshold = 0.25  # Confidence threshold
nmsThreshold = 0.4  # Non-maximum suppression threshold
inpWidth = 320  # Width of network's input image
inpHeight = 320  # Height of network's input image

# Give the configuration and weight files for the model and load the network using them.
modelConfiguration = "Yolo-Fastest-voc/yolo-fastest-xl.cfg"
modelWeights = "Yolo-Fastest-voc/yolo-fastest-xl.weights"

# modelConfiguration = "Yolo-Fastest-voc/yolo-fastest.cfg"
# modelWeights = "Yolo-Fastest-voc/yolo-fastest.weights"

# Load names of classes
classesFile = "voc.names"
classes = None
with open(classesFile, 'rt') as f:
    classes = f.read().rstrip('\n').split('\n')
colors = [np.random.randint(0, 255, size=3).tolist() for _ in range(len(classes))]


# print(classes)
# print(colors)
# Get the names of the output layers
def getOutputsNames(net):
    # Get the names of all the layers in the network
    layersNames = net.getLayerNames()
    # print(dir(net))
    # Get the names of the output layers, i.e. the layers with unconnected outputs
    return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]


# Draw the predicted bounding box
def drawPred(frame, classId, conf, left, top, right, bottom):
    # print(type(frame)) # <class 'numpy.ndarray'>
    # Draw a bounding box.
    cv.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), thickness=4)

    label = '%.2f' % conf
    log = ''
    # Get the label for the class name and its confidence
    if classes and classes[classId]=='person':
        # get("http://192.168.137.136:5000/OpenLED")
        assert (classId < len(classes))
        label = '%s:%s' % (classes[classId], label)
        # print(label)  # person:1.00
        log = time.strftime('\n' + "%Y-%m-%d %H:%M:%S", time.localtime()) + ',' + label + '\n'
        # print(log)
        # with open('Asava_pic/data.log', 'rw') as f:
        #     f.write(log)
    else:
        # get("http://192.168.137.136:5000/CloseLED")
        pass

    # Display the label at the top of the bounding box
    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    top = max(top, labelSize[1])
    # cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine), (255,255,255), cv.FILLED)
    cv.putText(frame, label, (left, top - 10), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), thickness=2)
    return log


# Remove the bounding boxes with low confidence using non-maxima suppression
def postprocess(frame, outs):
    frameHeight = frame.shape[0]
    frameWidth = frame.shape[1]

    classIds = []
    confidences = []
    boxes = []
    # Scan through all the bounding boxes output from the network and keep only the
    # ones with high confidence scores. Assign the box's class label as the class with the highest score.
    classIds = []
    confidences = []
    boxes = []
    for out in outs:
        for detection in out:
            scores = detection[5:]
            classId = np.argmax(scores)
            confidence = scores[classId]
            if confidence > confThreshold:
                center_x = int(detection[0] * frameWidth)
                center_y = int(detection[1] * frameHeight)
                width = int(detection[2] * frameWidth)
                height = int(detection[3] * frameHeight)
                left = int(center_x - width / 2)
                top = int(center_y - height / 2)
                classIds.append(classId)
                confidences.append(float(confidence))
                boxes.append([left, top, width, height])

    # Perform non maximum suppression to eliminate redundant overlapping boxes with
    # lower confidences.
    indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
    # print('indices',indices)
    # [[23]
    # [17]
    # [41]]
    # print('boxes',boxes)
    # print('classIds:',classIds)
    # print('confidences:',confidences)

    for i in indices:
        i = i[0]  # [23]
        box = boxes[i]  #
        left = box[0]
        top = box[1]
        width = box[2]
        height = box[3]
        try:
            log = drawPred(frame, classIds[i], confidences[i], left, top, left + width, top + height)
            cv.imwrite('Asava_pic/object.jpg', frame)
            with open('Asava_pic/data.log', 'a') as f:
                f.writelines(log)

            print(log)
            # print('draw!!')
        except:
            print('no draw!!')
            print('left:', left)
            print('top:', top)
            print('left + width:', left + width)
            print('top + height:', top + height)


if __name__ == '__main__':
    
    parser = argparse.ArgumentParser(description='Object Detection using YOLO in OPENCV')
    parser.add_argument('--image', type=str, default='a.png', help='Path to image file.')
    args = parser.parse_args()

    net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
    net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
    net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
    # Process inputs
    frame = cv.imread(args.image)
    # print('args.image',args.image) # args.image a.png
    # print(type(frame)) # <class 'numpy.ndarray'>

    # Create a 4D blob from a frame.
    blob = cv.dnn.blobFromImage(frame, 1 / 255.0, (inpWidth, inpHeight), [0, 0, 0], swapRB=False, crop=False)

    # Sets the input to the network
    net.setInput(blob)

    # Runs the forward pass to get output of the output layers
    outs = net.forward(getOutputsNames(net))

    # Remove the bounding boxes with low confidence
    postprocess(frame, outs)

    # Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
    t, _ = net.getPerfProfile()
    label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
    cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))

    winName = 'Deep learning object detection in OpenCV'
    cv.namedWindow(winName, 0)
    cv.imshow(winName, frame)
    cv.waitKey(0)
    cv.destroyAllWindows()
